package org.languageguide.readings;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.languageguide.user.User;

public  class SentenceTranslation{

	String sentence;
	String translation;
	Integer order;
	Integer id;
	Integer paragraph;
	boolean header;

	public SentenceTranslation(String sentence, String translation, Integer readingSentencesId, Integer paragraph, Integer sentenceOrder, boolean header){
		this.sentence =  sentence;
		this.translation = translation; 
		//this.order = order;
		this.id = readingSentencesId;
		this.paragraph = paragraph;
		this.order = sentenceOrder;
		this.header = header; 
	}

	public Integer getOrder(){
		return order - 1;	
	}
	
	public String getSentence(){
		
		return sentence;
	}
	
	public String getProcessedSentence(){
		if (header){
			return "<h2>" + sentence + "</h2>"; 
		}
		else{
			return sentence;
		}
		
	}
	
	public boolean isHeader(){
		return header;
	}

	public String getTranslation(){
		if (translation == null)
			return "";
		return translation;
	}

	public String getName(){
		return translation;
	}
	
	public Integer getParagraph(){
		return paragraph;
	}

	public void storeChange(Connection connection, String newTranslation, String target, User user, String ipAddress){
		if (translation == null && !newTranslation.equals("")){
			String Isql = "INSERT INTO ReadingSentenceTranslation (Translation, ReadingSentencesId, TargetLanguage, IpAddress) VALUES (?, ?, ?, ?)";
			PreparedStatement statement;
			try {
				statement = connection.prepareStatement(Isql);
				statement.setString(1, translation);
				statement.setInt(2, id);
				statement.setString(3, target);
				statement.setString(4, ipAddress);
				
				statement.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (!newTranslation.equals(translation)){
			//String Isql = "INSERT INTO ReadingSentenceTranslation (Translation, ReadingSentencesId, TargetLanguage) VALUES (?, ?, ?)";
			String sql = "UPDATE ReadingSentenceTranslation Set Translation = ? WHERE ReadingSentencesId = ? AND TargetLanguage = ?";
			PreparedStatement statement;
			try {
				statement = connection.prepareStatement(sql);
				statement.setString(1, translation);
				statement.setInt(2, id);
				statement.setString(3, target);
				statement.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}